/* Copyright 2009 (c) ENSI de Bourges
 * 		88 boulevard Lahitolle, 18020 Bourges Cedex, France
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <librsvg/rsvg.h>
#include <librsvg/rsvg-cairo.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "constants.h"
#include "frame-common.h"
#include "frame-utilities.h"
#include "machine.h"
#include "plugin.h"
#include "time-period.h"



char *get_display_name (frame_t *f)
{
	return "P0F";
}



void set_graph_type_pie (GtkMenuItem *item, gpointer *user_data)
{
	frame_t *f = (frame_t *) user_data;
	data_t *data = (data_t *) f->priv;

	data->graph_type = PIE;
	frame_request_content_refresh (f);
}



void set_graph_type_bar (GtkMenuItem *item, gpointer *user_data)
{
	frame_t *f = (frame_t *) user_data;
	data_t *data = (data_t *) f->priv;

	data->graph_type = BAR;
	frame_request_content_refresh (f);
}



GList *build_func_list ()
{
	GList *list = NULL;
	func_list_t *f1, *f2;

	// g_list_prepend is more efficient than g_list_append.
	f2 = malloc (sizeof (func_list_t));
	f2->name = strdup ("Show As Bars");
	f2->func = &set_graph_type_bar;
	list = g_list_prepend (list, f2);

	f1 = malloc (sizeof (func_list_t));
	f1->name = strdup ("Show As Pies");
	f1->func = &set_graph_type_pie;
	list = g_list_prepend (list, f1);
	
	return list;
}



GList *build_time_periods ()
{
	GList *list = NULL;
	time_period_t *p1, *p2, *p3, *p4;
	
	p1 = time_period_new (WEEK, 2);
	list = g_list_prepend (list, p1);
	
	p2 = time_period_new (DAY, 2);
	list = g_list_prepend (list, p2);
	
	p3 = time_period_new (HOUR, 6);
	list = g_list_prepend (list, p3);
	
	p4 = time_period_new (LIVE, 0);
	list = g_list_prepend (list, p4);
	
	return list;
}



int is_refreshing_needed (frame_t *f)
{
	int refreshing_needed = !(rand() % 5);
	char *path = frame_get_absolute_reports_path (f, "mon_rapport.txt");

	//You must now stat path to get it's mtime. Make sure to manage errors properly.

	g_free (path);
	return refreshing_needed;
}




int refresh_content (frame_t *f)
{
	data_t *data = (data_t *) f->priv;
	// All the code should be embed in if (f->compute) blocks. This is to ensure
	// the function will quit as fast as possible if it's asked not to compute
	// anymore.
	
	if (f->compute) {
		int a;
		//if type = bar, refresh_bar
		
		if (data->graph_type == PIE) {
			a=system ("ploticus OS.pl -png -o OS.png");
			
			if (a == -1) {
				fprintf (stderr, "Erreur lors de la génération de l'image ploticus");
				return -1;
			}
		} else if (data->graph_type == BAR) {
			a=system ("ploticus OS2.pl -png -o OS.png");
			
			if (a == -1) {
				fprintf (stderr, "Erreur lors de la génération de l'image ploticus");
				return -1;
			}
		}
}
	if (f->compute) {
		printf ("The ploticus image has been recomputed, now we should redraw the drawing area.\n");
		frame_request_redraw (f);
	}

	return 0;
}



int init_private (frame_t *f)
{
	f->priv = malloc (sizeof (data_t));
	// Recommended to access f->priv
	data_t *data = (data_t *) f->priv;
	
	data->graph_type = PIE;
	return 0;
}



int free_private (frame_t *f)
{
	// Recommended to access f->priv
	data_t *data = (data_t *) f->priv;
	
	free (data);

	return 0;
}



int display_machine_changed (frame_t *f)
{
	printf ("Here, I must change my internal data to reflect the machine change.\n");
	frame_request_content_refresh (f);
	return 0;
}



int time_period_changed (frame_t *f)
{
	printf ("Here, I must change my internal data to reflect the time period change.\n");
	frame_request_content_refresh (f);
	return 0;
}



int frame_size_changed (frame_t *f)
{
	printf ("Here, I must change my internal data to reflect the frame size change.\n");
	frame_request_content_refresh (f);
	return 0;
}



/* À voir
 * CAIRO
 * 	http://library.gnome.org/devel/cairo/stable/cairo-context.html
 * 	http://www.cairographics.org/manual/cairo-transformations.html
 *
 * IMAGES PNG AVEC CAIRO
 * 	http://library.gnome.org/devel/cairo/stable/cairo-image-surface.html
 *
 * IMAGES SVG AVEC CAIRO
 * 	http://library.gnome.org/devel/rsvg/stable/rsvg-Core-API.html
 * 	http://library.gnome.org/devel/rsvg/stable/rsvg-Cairo.html
 * 
 */
int draw_area (frame_t *f, cairo_t *cr)
{
	int				w,h;
	double				tmp;
	cairo_surface_t			*image		= NULL;

	/* Importe une image png d'un fichier, stockant sa taille dans deux variables */
	image = cairo_image_surface_create_from_png ("OS.png");
	w = cairo_image_surface_get_width (image);
	h = cairo_image_surface_get_height (image);
	tmp = frame_get_height (f) / h;
	
	/* Sauvegarde la configuration du masque */
	cairo_save (cr);
	/* Redimensionnne le masque pour y coller un bout de tux */
	cairo_scale (cr, tmp, tmp);
	/* Définit l'image comme source */
	cairo_set_source_surface (cr, image, 0, 0);
	/* Peint le bout de tux sur la surface */
	cairo_paint (cr);
	/* Libère la mémoire allouée pour le bout de tux */
	cairo_surface_destroy (image);
	/* Restore la configuration du masque */
	cairo_restore (cr);


	return 0;
}
